package org.swingDemo.UI;

import cn.hutool.core.util.NumberUtil;
import com.esotericsoftware.tablelayout.swing.Table;
import org.swingDemo.helper.TicketTypeHelper;

import javax.swing.*;
import java.awt.*;

public class ConfigPanel extends JPanel {

    private JTextField countField;
    private JRadioButton welfareLotteryRadioButton;

    public ConfigPanel() {
        super(new BorderLayout(5, 5));
        initPanel();
    }

    // 获取执行次数
    public int getCount() {
        String text = countField.getText();
        if (!NumberUtil.isInteger(text)) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字");
            return -1;
        }
        return NumberUtil.parseInt(text);
    }

    // 获取彩票类型
    public int getTicketType() {
        if (welfareLotteryRadioButton.isSelected()) {
            return TicketTypeHelper.WelfareLottery;
        }
        return -1;
    }

    private void initPanel() {
        countField = new JTextField();
        countField.setText("10000");

        welfareLotteryRadioButton = new JRadioButton("福利彩票");
        welfareLotteryRadioButton.setSelected(true);

        Table table1 = new Table();
        table1.addCell("执行次数:");
        table1.addCell(countField).expandX().fillX();

        Table table2 = new Table();
        table2.addCell("彩票类型:");
        table2.addCell(welfareLotteryRadioButton).left().expandX().fillX();

        this.setBorder(BorderFactory.createTitledBorder("配置选项"));
        this.add(table1, BorderLayout.PAGE_START);
        this.add(table2, BorderLayout.CENTER);
    }
}
